﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Compiler.LabForms
{
    public partial class TetradeEdit : Form
    {
        private ListView _tetradeView;
        private ListViewItem _item;

        public TetradeEdit(ListView tetradeView)
        {
            _tetradeView = tetradeView;
            InitializeComponent();
            Text = @"Добавление тетрады";
        }

        public TetradeEdit(ListView tetradeView, ListViewItem item)
        {
            _tetradeView = tetradeView;
            _item = item;
            InitializeComponent();
            Text = @"Редактирование тетрады";
            operationBox.Text = _item.Text;
            operandOneBox.Text = _item.SubItems[1].Text;
            operandTwoBox.Text = _item.SubItems[2].Text;
            resultBox.Text = _item.SubItems[3].Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(operationBox.Text)) return;
            if (_item != null)
            {
                _item.Text = operationBox.Text;
                _item.SubItems[1].Text = operandOneBox.Text;
                _item.SubItems[2].Text = operandTwoBox.Text;
                _item.SubItems[3].Text = resultBox.Text;
            }
            else
            {
                var newItem = new ListViewItem
                {
                    Text = operationBox.Text
                };
                newItem.SubItems.Add(operandOneBox.Text);
                newItem.SubItems.Add(operandTwoBox.Text);
                newItem.SubItems.Add(resultBox.Text);
                _tetradeView.Items.Add(newItem);
            }
            _tetradeView.SelectedItems.Clear();
            Close();
        }
    }
}
